package com.rocksea.app.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.rocksea.app.R;
import com.rocksea.app.db.StaticLoad.StaticLoad;
import com.rocksea.app.entry.LoadDataEvent;
import com.rocksea.app.tasks.Temp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

/**
 *
 * @author 吴志华
 * @date 2017/10/26
 *
 * @FileName:com.rocksea.app.fragment.StaticLoadLogFrament.java
 */

public class StaticLoadLogFrament extends CommonBaseInfoFragment {
    public static StaticLoadLogFrament newInstance(Bundle mBundle) {
        StaticLoadLogFrament frament = new StaticLoadLogFrament();
        frament.setArguments(mBundle);
        EventBus.getDefault().register(frament);
        return frament;
    }

    @Override
    protected void requestAgain() {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loadDataEvent(LoadDataEvent event)
    {
        initData();
    }

    @Override
    protected void initData() {
        loading();
        StaticLoad mStaticLoad = Temp.getStaticLoad();
        if (mStaticLoad==null)
        {
            loadFailed();
            mErrorText.setText("暂无测试日志");
            return;
        }
        ArrayList<StaticLoad.StaticLoadLog> mLoadLogs = mStaticLoad.mStaticLoadLog;
        if (mLoadLogs==null)
        {
            loadFailed();
            mErrorText.setText("暂无测试日志");
            return;
        }
        if (mLoadLogs.size()==0)
        {
            loadFailed();
            mErrorText.setText("暂无测试日志");
            return;
        }
        LogAdpter logAdpter = new LogAdpter(mLoadLogs,getContext());
        pileInfoList.setAdapter(logAdpter);
        loadCompleted();
    }
    private class LogAdpter extends RecyclerView.Adapter<StaticLoadLogFrament.ViewHolder>
    {
        private ArrayList<StaticLoad.StaticLoadLog> mLoadLogs;
        private Context context;
        private Animation animation;
        private Animation endAnimation;

        public LogAdpter(ArrayList<StaticLoad.StaticLoadLog> mLoadLogs, Context context) {
            this.mLoadLogs = mLoadLogs;
            this.context = context;
            animation = new RotateAnimation(
                    0f,
                    -90f,
                    RotateAnimation.RELATIVE_TO_SELF,
                    0.5f,
                    RotateAnimation.RELATIVE_TO_SELF,
                    0.5f);
            animation.setDuration(200);
            animation.setFillAfter(true);
            animation.setInterpolator(new LinearInterpolator());

            endAnimation = new RotateAnimation(
                    -90f,
                    0f,
                    RotateAnimation.RELATIVE_TO_SELF,
                    0.5f,
                    RotateAnimation.RELATIVE_TO_SELF,
                    0.5f);
            endAnimation.setDuration(200);
            endAnimation.setFillAfter(true);
            endAnimation.setInterpolator(new LinearInterpolator());
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View mBootView = LayoutInflater.from(context).inflate(R.layout.item_log_layout,viewGroup,false);
            return new ViewHolder(mBootView);
        }
        @Override
        public void onBindViewHolder(final ViewHolder viewHolder, int i)
        {
            StaticLoad.StaticLoadLog loadLog = mLoadLogs.get(i);
            viewHolder.tvNumber.setText(String.format("%d",i+1));
            viewHolder.tvEventTime.setText(loadLog.eventTime.split(" ")[1]);
            viewHolder.tvEventInfo.setText(loadLog.eventInfo);
            viewHolder.tvRemake.setText(loadLog.remark);
            viewHolder.ItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (viewHolder.textLayout.isShown())
                    {
                        viewHolder.textLayout.setVisibility(View.GONE);
                        viewHolder.imageView.startAnimation(endAnimation);
                    }
                    else
                    {
                        viewHolder.textLayout.setVisibility(View.VISIBLE);
                        viewHolder.imageView.startAnimation(animation);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mLoadLogs.size();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public TextView tvNumber,tvEventInfo,tvEventTime,tvRemake;
        public View textLayout,ItemView;
        public ImageView imageView;
        public ViewHolder(View itemView)
        {
            super(itemView);
            tvNumber = (TextView) itemView.findViewById(R.id.tvNumber);
            tvEventInfo =  (TextView)itemView.findViewById(R.id.tvEventInfo);

            tvEventTime =  (TextView)itemView.findViewById(R.id.tvEventTime);
            tvRemake = (TextView) itemView.findViewById(R.id.tvRemake);
            textLayout = itemView.findViewById(R.id.textLayout);
            ItemView = itemView.findViewById(R.id.ItemView);
            imageView = (ImageView) itemView.findViewById(R.id.ImageView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private String getProcsess(String remake) throws JSONException {
        JSONObject jsonObject = new JSONObject(remake);
        return jsonObject.getJSONObject("静载试验日志").getString("试验进程");
    }
}
